package com.servlets.user.role;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.GlobalConstants;
import com.core.ListUtils;
import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.model.mapparser.UserRoleMapParser;
import com.model.user.UserRole;
import com.servicelayer.user.UserRoleListLayer;
import com.servlets.TaskTrackerServlet;

/**
 * Servlet implementation class UserRoleUpdate
 */
public class UserRoleUpdate
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

    private static final long   serialVersionUID = 1L;
    private Integer             roleId           = null;
    private HttpServletResponse postResponse     = null;



    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserRoleUpdate() {

        super();

    }



    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet ( HttpServletRequest request,
                           HttpServletResponse response )
            throws ServletException, IOException {

        this.roleId = null;
        try {
            this.roleId = Integer.parseInt(request.getParameter(GlobalConstants.SERVLET_PARAM_KEY));
        } catch ( NumberFormatException e ) {
            this.roleId = null;
        }
        super.doGet(request, response);
    }



    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @SuppressWarnings("unchecked")
    protected void doPost ( HttpServletRequest request,
                            HttpServletResponse response )
            throws ServletException, IOException {

        this.postResponse = response;

        Map <String, String[]> map = request.getParameterMap();
        UserRole template = UserRoleMapParser.fromMap(map);



        UserRoleListLayer roleListLayer = new UserRoleListLayer();
        roleListLayer.mergeEntityByIDWithTemplate(this.roleId, template, this);



    }



    @Override
    public <T> void responseResponseItemsList ( final List <T> items,
                                                final Object request,
                                                final Exception exception ) {

        T item = ListUtils.listItemAtIndex(items, 0);

        this.redirectItemIntoOpenServlet(item);

    }



    private <T> void redirectItemIntoOpenServlet ( T item ) {

        if ( this.postResponse == null ) {
            return;
        }

        String redirection = String.format("%s", GlobalConstants.SERVLET_PROJECTLIST);

        if ( item != null ) {
            if ( item instanceof IKVObject ) {
                IKVObject kvObject = (IKVObject) item;
                String idValue = kvObject.idValue().toString();
                redirection = String.format("%s?%s", GlobalConstants.SERVLET_ROLEINFO, idValue);
            }
        }

        try {
            this.postResponse.sendRedirect(redirection);
        } catch ( IOException e ) {
            e.printStackTrace();
        }

    }

}
